//
//  MyOrderSubVc.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/18.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MyOrderSubVc.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "OrderCell.h"
#import "CustomerOrderDetailVc.h"
#import "OrderDetailVc.h"
#import "NSString+category.h"




@interface MyOrderSubVc ()
@property (nonatomic,strong)OrderDM *dm;
@property (nonatomic,strong)NSArray *dataArray;


@end

@implementation MyOrderSubVc
@synthesize state,dm,dataArray;


- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self initView];
    
    
    [self refreshDataWithCache:true];
}



-(void)initView{
    
    [super initView];

    
    dm = [[OrderDM alloc]init];
    
    
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}


-(void)btnOnClick:(UIButton *)sender{
    

    if(sender.tag==56){
        [self refreshData];
    }
}



#pragma mark pullTableView delegate
-(void)refreshData{
    [super refreshData];
    
    [self refreshDataWithCache:false];
}



- (void)loadMore{
    [super loadMore];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm loadMoreData:state];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                
            }else if (mod.resltState == ResultFail){//服务器返回的code出错
                
                
            }else{
                
                NSMutableArray *array = [[NSMutableArray alloc]initWithArray:bself.dataArray];
                [array addObjectsFromArray:mod.dataMod];
                bself.dataArray = [[NSArray alloc]initWithArray:array];
                
            }
            
            [bself.tableView reloadData];
            
            [bself.tableView.footer endRefreshing];
            
            
        });
    });
}




-(void)refreshDataWithCache:(BOOL)cache{
    
    
    [self.nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod =[dm refreshData:state WithCache:cache];
        

        dispatch_async(dispatch_get_main_queue(), ^{
            
            bself.dataArray = mod.dataMod;
            
            int count = 0;
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                if(bself.dataArray.count==0){//没数据
                    
                    count=0;
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                    
                }else{//有数据
                    
                    
                    NSDictionary *resDic = mod.resultDic;
                    NSDictionary *abstractResponseDic = resDic[@"orderListDto"];
                    if(![abstractResponseDic[@"orderCount"] isKindOfClass:[NSNull class]]){
                        count = [abstractResponseDic[@"orderCount"] intValue];
                    }else{
                        count = 0;
                    }
                                        
                    dataArray = mod.dataMod;
                    
                    [bself.nodataCell setState:nodataCellStateNorMal];
                }
                
                
                if (state==Order_State_ing) {
                    
                    [bself.countLab setText:[NSString stringWithFormat:@"交易中(%d)",count]];
//                    [bself.countLab setAttributedText:[NSString makeLineStyle:[NSString stringWithFormat:@"交易中(%d)",count] color:[UIColor redColor] fontSize:14]];
                }else if (state==Order_State_finish){
                    
                    [bself.countLab setText:[NSString stringWithFormat:@"已完成(%d)",count]];
//                    [bself.countLab setAttributedText:[NSString makeLineStyle:[NSString stringWithFormat:@"已完成(%d)",count] color:[UIColor redColor] fontSize:14]];

                }else{
                    
                    [bself.countLab setText:[NSString stringWithFormat:@"全部(%d)",count]];
//                    [bself.countLab setAttributedText:[NSString makeLineStyle:[NSString stringWithFormat:@"全部(%d)",count] color:[UIColor redColor] fontSize:14]];
                }
            }
            
            [bself.tableView reloadData];
            
            [bself.tableView.header endRefreshing];
        });
    });
    
}





#pragma tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count==0) {
        if(!self.nodataCell){
            self.nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
            [self.nodataCell setState:nodataCellStateLoading];
            [[self.nodataCell refreshBtn]setTag:56];
            [self.nodataCell.refreshBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return self.nodataCell;
    }
    
    
    
    OrderCell *cell = [OrderCell cellWithTableView:tableView];
    
    [cell setData:[dataArray objectAtIndex:indexPath.row]];
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (dataArray&&dataArray.count>0) {
        
        return [OrderCell getHight:nil withTableView:tableView];
    }
    
    return self.tableView.frame.size.height;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count>0) {
        
        OrderDetailVc *vc = [[OrderDetailVc alloc]init];
        OrderData *data = [dataArray objectAtIndex:indexPath.row];
        vc.orderCode = data.orderCode;
         vc.hidesBottomBarWhenPushed = YES;
        [self.nvc pushViewController:vc animated:YES];

    }
}







@end
